package com.cn.springcloudwechat.controller.vaccine.log;


import com.cn.springcloudwechat.controller.base.BaseWeChatController;
import com.cn.springcloudwechat.model.vaccine.OperateLog;
import com.cn.springcloudwechat.service.vaccine.log.OperateLogService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日志工具类
 *
 * @author Melody
 * @date 2018年3月26日 下午4:29:13
 */
@Aspect
@Component
public class LogAspect extends BaseWeChatController {

	private static Log log = LogFactory.getLog(LogAspect.class);

	@Autowired
	private OperateLogService operateLogService;
	//方法执行前调用，插入包下所有业务方法
	/*@Before("execution (* com.cn.controller.baseRegister.*.*(..))")
	public void printLog() {
		System.out.println("业务方法执行前日志。");
	}*/
	@Around("@annotation(com.cn.springcloudwechat.controller.vaccine.log.LogInfo)")
	public Object process(ProceedingJoinPoint proceedingJoinPoint) {
		System.out.println("+++++++++++++++++++++++++++++++++++++================");
		Object object = null;
		MethodSignature ms = (MethodSignature) proceedingJoinPoint.getSignature();
		// 获取方法
		Method method = ms.getMethod();
		// 获取类名
		String className = proceedingJoinPoint.getTarget().getClass().getName();
		// 获取注解
		LogInfo logInfo =  method.getAnnotation(LogInfo.class);
		// 获取操作类型
		OperateType operateType = logInfo.operateType();
		// 获取用户类型
		UserType[] userTypes = logInfo.userType();
		// 获取操作名
		String operateName = operateType.getValue();
		// 获取备注
		String content = logInfo.content();
		OperateLog operateLog = new OperateLog();
		operateLog.setContent("[" + content + "] [操作位置]:[" + className + ":" + method.getName() + "]");
		operateLog.setType(operateType.getId());
		operateLog.setOperateDate(new Date());
		for (int i = 0; i < userTypes.length; i++) {
			try {
				operateLog.setOperaterId(getUserInfo().getUserId());
				operateLog.setOperaterName(getUserInfo().getNickname());
			} catch (Exception e) {
				System.out.println("后台日志");
				try {
					//todo 后台记录

				} catch (Exception e2) {
					/*try {
						HttpServletResponse response = ThreadLocalUtil.getResponse();
						response.setContentType("text/json; charset=UTF-8");
						response.setCharacterEncoding("utf-8");
						AjaxRes ajaxRes = new AjaxRes();
						ajaxRes.setLogoutMsg(ErrorUtil.LOGIN_EXPIRE);
						response.getWriter().write(ajaxRes.toJSONString());
						response.getWriter().close();
					} catch (IOException e1) {
						e1.printStackTrace();
					}*/
				}
			}
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(operateLog.getOperateDate());
		try {
			object = proceedingJoinPoint.proceed();
			operateLog.setContent(operateLog.getContent() + " [操作成功]");
		} catch (Throwable e) {
			operateLog.setContent(operateLog.getContent() + " [操作失败]:[" + e.getMessage() + "]");
		}
		// 打印日志
		log.info("[" + operateLog.getOperaterName() + "]在[" + dateString + "][" + operateName + "]" + operateLog.getContent());
		try {
            this.operateLogService.insert(operateLog);
        }catch (Exception E){
            System.out.println(E.getMessage());
        }

		return object;
	}
}
